package com.itheima.controller;


import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    //查找服务
    @Autowired
    private MenuService menuService;


    //分页查询
    @RequestMapping("/pageQuery")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;
    }


    //用于角色的新增或者编辑,查询出所有的菜单,展示再表单中
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Menu> data =  menuService.findAll();
        return Result.success(data, MessageConstant.QUERY_MENU_SUCCESS);
    }

    @GetMapping("/findParentIdsByMenuId")
    public List<Integer> findParentIdsByMenuId(Integer mid) {
        return this.menuService.findParentIdsByMenuId(mid);
    }

    //新增一个菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu, Integer id) {
        if (id == 999999) {
            menu.setParentMenuId(null);
        } else {
            menu.setParentMenuId(id);
        }
        menuService.add(menu);
        return Result.success( MessageConstant.ADD_MENU_SUCCESS);
    }

    //编辑菜单用于数据的回显
    @RequestMapping("/findById")
    public Result findByPath(Integer id) {
        Menu data = menuService.findById(id);
        return Result.success(data, MessageConstant.QUERY_MENU_SUCCESS);
    }

    //编辑一个菜单
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu, Integer id) {
        if (id == 999999) {
            menu.setParentMenuId(null);
            menuService.edit(menu);
        } else {
            menu.setParentMenuId(id);
            menuService.edit(menu);
        }
        return Result.success( MessageConstant.EDIT_MENU_SUCCESS);
    }

    //删除一个菜单
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        menuService.delete(id);
        return Result.success( MessageConstant.DELETE_MENU_SUCCESS);
    }

    //用于查找所有的自关联的id
    @RequestMapping("/findMenuparentMenuId")
    public Result findMenuparentMenuId() {
        List<Map> data = menuService.findMenuparentMenuId();
        return Result.success(data, MessageConstant.QUERY_MENU_SUCCESS);
    }


    //用于查找所有的自关联的id
    @RequestMapping("/findMenuIdAndDisabled")
    public Result findMenuIdAndDisabled(Integer id) {
        List<Map> data = menuService.findMenuIdAndDisabled(id);
        return Result.success(data, MessageConstant.QUERY_MENU_SUCCESS);
    }


}
